package com.xyw.his00.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Slf4j
@Aspect
@Component
@Order(10)
public class TimeAspect {
    @Pointcut("execution(* com.xyw.his00.service.*.*(..))")
    private void pointcut(){

    }
    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        //1.获取目标对象的类名
        joinPoint.getClass().getName();

        //2.获取目标方法的方法名
        String methodName=joinPoint.getSignature().getName();
        //3.获取目标方法运行时的参数
       Object[] args= joinPoint.getArgs();
        //4.放行目标方法执行

        //5.获取目标方法运行的返回值
        //6.
        System.out.println("before------");
    }
    @Around("pointcut()")
    public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        //1.获取目标对象的类名
        proceedingJoinPoint.getTarget().getClass().getName();

        //2.获取目标方法的方法名
        String methodName=proceedingJoinPoint.getSignature().getName();
        //3.获取目标方法运行时的参数
        Object[] args= proceedingJoinPoint.getArgs();

        System.out.println("around before方法执行");
        long begin = System.currentTimeMillis();
        //4.放行方法
        Object object = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        long duration = end - begin; // 计算耗时
        System.out.println("around after方法执行");
        // 使用占位符{}来格式化日志消息，并将耗时作为参数传递
        log.info("{}执行耗时: {} ms", proceedingJoinPoint.getSignature(), duration);
       //返回方法返回值
        return object;
    }
    @After("pointcut()")
    public void after(){
        System.out.println("after_____");
    }
    @AfterReturning("pointcut()")
    public void afterThrowing(){
        System.out.println("-----afterReturning---------------");
    }
    @AfterThrowing("pointcut()")
    public void afterReturning(){
        System.out.println("afterReturing___________");
    }

}
